package com.sun.services.roles.repository;


import com.sun.entitys.users.Role;
import com.sun.dto.users.RoleDto;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * MP自动填充Handler
 *
 * @Author whsunchunbo@qq.com
 * @CreateTime 2025/9/9 03:10
 */
public interface RoleRepository extends CrudRepository<Role, Long> {
    @Query("""
            SELECT  new com.sun.dto.users.RoleDto(r.id,
                          r.name, r.code, r.description, r.createTime,
                          r.updateTime, r.createrName,
                          r.updaterName, r.createrId, r.updaterId)   FROM Role r left  join UserRole ur on r.id = ur.roleId
            
            WHERE ur.userId = :userId and   ur.deleted = 0 and r.deleted = 0
            """)
    List<RoleDto> findByUserId(Long userId);

    @Query("""
            SELECT  new com.sun.dto.users.RoleDto(r.id,  r.name, r.code,
             r.description, r.createTime, r.updateTime,
              r.createrName, r.updaterName, r.createrId, r.updaterId)   FROM Role r where  r.deleted = 0 and  r.id = :roleId
            """)
    RoleDto findByRoleId(Long roleId);


}
